package com.ruoyi.web.controller.smile;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Pattern;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.smile.mapper.McDynamicCommentMapperPlus;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.smile.domain.McDynamicComment;
import com.ruoyi.smile.service.IMcDynamicCommentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 动态评论Controller
 *
 * @author xin
 * @date 2025-03-22
 */
@RestController
@RequestMapping("/smile/comment")
public class McDynamicCommentController extends BaseController {
    @Autowired
    private IMcDynamicCommentService mcDynamicCommentService;

    @Autowired
    private McDynamicCommentMapperPlus mcDynamicCommentMapper2;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询动态评论列表
     */
    @PreAuthorize("@ss.hasPermi('smile:comment:list')")
    @GetMapping("/list")
    public TableDataInfo list(McDynamicComment mcDynamicComment) {
        startPage();
        List<McDynamicComment> list = mcDynamicCommentService.selectMcDynamicCommentList(mcDynamicComment);
        return getDataTable(list);
    }

    /**
     * 查询一级动态评论列表
     */
    @PreAuthorize("@ss.hasPermi('smile:comment:list')")
    @GetMapping("/list2")
    public TableDataInfo list2(McDynamicComment mcDynamicComment) {
        startPage();
        List<McDynamicComment> list = mcDynamicCommentMapper2.list1(mcDynamicComment.getBeCommentId());
        list.forEach(it -> {
            //二级评论数
            it.setBeCommentCount(mcDynamicCommentMapper2.getCommentNum(it.getId(), it.getBeCommentId()));
            //当前点赞数
            it.setLikeCount(mcDynamicCommentMapper2.getCommentLikeNum(it.getBeCommentId(), it.getId()));
            //用户信息
            it.setUserInfo(userService.selectUserById(it.getUserId()));
            //请求者是否点赞
            LoginUser loginUser = getLoginUser();
            if (loginUser.getUserId() != null)
                it.setIsLike(mcDynamicCommentMapper2.getCommentCommentIsLike(it.getBeCommentId(), it.getId(), loginUser.getUserId()) == 0 ? "0" : "1");
        });
        return getDataTable(list);
    }
    /**
     * 查询二级动态评论列表
     */
    @PreAuthorize("@ss.hasPermi('smile:comment:list')")
    @GetMapping("/list3")
    public TableDataInfo list3(McDynamicComment mcDynamicComment) {
        startPage();
        List<McDynamicComment> list = mcDynamicCommentMapper2.list2(mcDynamicComment.getRootId(),mcDynamicComment.getBeCommentId());
        list.forEach(it -> {
            //当前点赞数
            it.setLikeCount(mcDynamicCommentMapper2.getCommentLikeNum(it.getBeCommentId(), it.getId()));
            //用户信息
            it.setUserInfo(userService.selectUserById(it.getUserId()));
            //请求者是否点赞
            LoginUser loginUser = getLoginUser();
            if (loginUser.getUserId() != null)
                it.setIsLike(mcDynamicCommentMapper2.getCommentCommentIsLike(it.getBeCommentId(), it.getId(), loginUser.getUserId()) == 0 ? "0" : "1");
        });
        return getDataTable(list);
    }

    /**
     * 导出动态评论列表
     */
    @PreAuthorize("@ss.hasPermi('smile:comment:export')")
    @Log(title = "动态评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, McDynamicComment mcDynamicComment) {
        List<McDynamicComment> list = mcDynamicCommentService.selectMcDynamicCommentList(mcDynamicComment);
        ExcelUtil<McDynamicComment> util = new ExcelUtil<McDynamicComment>(McDynamicComment.class);
        util.exportExcel(response, list, "动态评论数据");
    }

    /**
     * 获取动态评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('smile:comment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mcDynamicCommentService.selectMcDynamicCommentById(id));
    }

    /**
     * 新增动态评论
     */
    @PreAuthorize("@ss.hasPermi('smile:comment:add')")
    @Log(title = "动态评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody McDynamicComment mcDynamicComment) {
        return toAjax(mcDynamicCommentService.insertMcDynamicComment(mcDynamicComment));
    }

    /**
     * 修改动态评论
     */
    @PreAuthorize("@ss.hasPermi('smile:comment:edit')")
    @Log(title = "动态评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody McDynamicComment mcDynamicComment) {
        return toAjax(mcDynamicCommentService.updateMcDynamicComment(mcDynamicComment));
    }

    /**
     * 删除动态评论
     */
    @PreAuthorize("@ss.hasPermi('smile:comment:remove')")
    @Log(title = "动态评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mcDynamicCommentService.deleteMcDynamicCommentByIds(ids));
    }
}
